package one.bugu.android.demon.ui.activity.shop;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.common.util.ToastUtils;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.MagicCalBuyBean;
import one.bugu.android.demon.bean.MagicCalDetailBean;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseActivity;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.adapter.shop.OrderManagerAdapter;
import one.bugu.android.demon.ui.refresh.SmartRefreshLayout;
import one.bugu.android.demon.ui.refresh.listener.OnRefreshLoadmoreListener;
import one.bugu.android.demon.ui.refresh.listener.RefreshLayout;
import one.bugu.android.demon.ui.widget.BaseTopBarView;
import one.bugu.android.demon.util.IntentUtils;
import one.bugu.android.demon.util.PreferencesUtil;
import one.bugu.android.demon.util.StatusBarUtils;

/**
 * 订单管理界面
 */
@LKContentView(R.layout.activity_order_manager)
public class OrderManagerActivity extends MyBaseActivity {
    /**
     * topBar
     */
    @LKInjectView(R.id.btbv_qp_top_bar)
    private BaseTopBarView btbv_qp_top_bar;

    /**
     * 列表
     */
    @LKInjectView(R.id.lv_qp_list)
    private ListView lv_qp_list;

    /**
     * 缺省页
     */
    @LKInjectView(R.id.ll_empty_view)
    private LinearLayout ll_empty_view;

    /**
     * 刷新控件
     */
    @LKInjectView(R.id.ref_layout)
    private SmartRefreshLayout ref_layout;

    /**
     * 数据适配器
     */
    private OrderManagerAdapter mAdapter;

    private int offset = 1;

    private int limit = 20;

    private String token;

    @Override
    protected void initView() {
        super.initView();
        //状态栏
        StatusBarUtils.getInstance().setStatusBar("#FFFFFF", OrderManagerActivity.this, btbv_qp_top_bar, false);

        mAdapter = new OrderManagerAdapter(this, null);
        lv_qp_list.setAdapter(mAdapter);

        ref_layout.setEnableRefresh(true);//启用刷新
        ref_layout.setEnableLoadmore(true);//启用加载

    }

    @Override
    protected void initData() {
        super.initData();
        token = PreferencesUtil.getInstance().getString(OrderManagerActivity.this, Constant.TOKEN);
        offset = 1;
        getData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        //topBar事件处理
        btbv_qp_top_bar.setEventInterface(new BaseTopBarView.EventInterface() {
            @Override
            public void leftOnClick() {
                finish();
            }

            @Override
            public void leftCloseClick() {

            }

            @Override
            public void rightOnClick() {

            }
        });

        //刷新加载
        ref_layout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                //加载数据
                offset++;
                getData();
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                //刷新数据
                offset = 1;
                getData();
            }
        });

        //条目点击
        lv_qp_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MagicCalDetailBean magicCalDetailBean = mAdapter.allData().get(position);
                if (magicCalDetailBean != null) {
                    Intent intent = new Intent(OrderManagerActivity.this, OrderDetailsActivity.class);
                    intent.putExtra("ORDER_DATA", magicCalDetailBean);
                    startActivity(intent);
                }
            }
        });
    }

    /**
     * 获取数据
     */
    private void getData() {
        System.out.println("加载   页数 == " + offset);
        Map<String, String> map = new HashMap<>();
        map.put("token", token);
        map.put("offset", String.valueOf(offset));
        map.put("limit", String.valueOf(limit));
        LKUtil.getHttpManager().postBody(HttpConstant.MAGIC_CAL_ORDER, map, handler);
    }

    /**
     * 获取算力信息的网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<List<MagicCalDetailBean>> handler = new BaseHttpAsycResponceHandler<List<MagicCalDetailBean>>(true) {
        @SuppressLint("SetTextI18n")
        @Override
        public void onSuccess(List<MagicCalDetailBean> data) {
            super.onSuccess(data);
            ref_layout.finishLoadmore();
            ref_layout.finishRefresh();
            if (!data.isEmpty()) {
                if (offset == 1) {
                    mAdapter.clear();
                }
                mAdapter.addAll((ArrayList<MagicCalDetailBean>) data);
            }

            boolean canLoad = data.size() >= limit;
            ref_layout.setEnableLoadmore(canLoad);
            setEmptyView();
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);
            ref_layout.finishLoadmore();
            ref_layout.finishRefresh();
            setEmptyView();
            ToastUtils.custom(message);
        }

        @Override
        public void onError() {
            super.onError();
            ref_layout.finishLoadmore();
            ref_layout.finishRefresh();
            setEmptyView();
        }
    };


    /**
     * 设置显示缺省页
     */
    private void setEmptyView() {
        boolean showEmptyView = mAdapter != null && mAdapter.getCount() > 0;
        ref_layout.setVisibility(showEmptyView ? View.VISIBLE : View.GONE);
        ll_empty_view.setVisibility(showEmptyView ? View.GONE : View.VISIBLE);
    }

}
